//index.js
const Controllers = require('../controllers/Controller.js').controller;
const app = getApp();
Page({
  data: {
    globalStype:'',
    tabbar:[],
    shopId:'',
    shopDetailsdata:'',
    shopRotationImgdata:'',
    shopGoodsPagedata:[],
    pageNumber:1,
    pageSize:5,
    isEnd:false,
    type:0,
    myInformationdata:{
      vip:0
    },
    isloading:true,
    isFixedTop:false,
    inputValue:'',
    imgheights: [],
    //图片宽度 
    imgwidth: 750,
    //默认  
    current: 0,
    navheight:0,
    headerheight:0,
    barheight:0
  },
  onLoad(option) {
    this.setData({globalStype:app.globalData.globalStype})
    let shopId=''
    if(app.globalData.shopId){
      shopId=app.globalData.shopId
      wx.setStorageSync('shopId', shopId)
    }
    if(option.shopId){
      console.log(option)
      shopId=option.shopId
      app.globalData.shopId=option.shopId
      wx.setStorageSync('shopId', shopId)
    }
    if(option.scene){
      shopId=decodeURIComponent(option.scene);
      app.globalData.shopId=decodeURIComponent(option.scene);
      wx.setStorageSync('shopId', shopId)
    }
    if(shopId){
      this.setData({
        shopId
      })
      this.shopDetails()
    }
    console.log(shopId)
    this.setData({
      tabbar: this.data.globalStype=='pinStyle'?app.globalData.tabbar:app.globalData.tabbar2,
    });

    this.onSubscribeMsg();

  },
  onSubscribeMsg(){
    wx.requestSubscribeMessage({
      tmplIds:['CgSkX1UN123rlfAQ9zXiMyfiF1ugDkOsd8yLmvcbHt0','IoewIlKNaUfOLOhKUOdPKMOnJxdtdqWGfOotuOB2R70','TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'],//'TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'
      success:res=>{
        console.log(res)
      },
      complete:()=>{
      }
    })
  },
  onShow(){
    if (wx.getStorageSync('usertoken')) {
        //加载首页数据
        this.myInformation()
    }
    //无usertoken，等待login登录回调
    app.usertokenReadyCallback = res => {
      if(res.data.sessionId){
        this.myInformation()
      }
    }
    
  },
  imageLoad: function (e) {//获取图片真实宽度  
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比  
      ratio = imgwidth / imgheight;
      console.log(imgwidth, imgheight)
    //计算的高度值  
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight;
    var imgheights = this.data.imgheights;
    //把每一张图片的对应的高度记录到数组里  
    imgheights[e.target.dataset.id] = imgheight;
    this.setData({
      imgheights: imgheights
    })
    setTimeout(res=>{
      this.getfixheight()
    },1000)
  },
  bindchange: function (e) {
    // console.log(e.detail.current)
    this.setData({ current: e.detail.current })
  },
  tabChange(e){
    var index=e.detail.index;
    if(index==1){
      wx.requestSubscribeMessage({
        tmplIds:['CgSkX1UN123rlfAQ9zXiMyfiF1ugDkOsd8yLmvcbHt0','IoewIlKNaUfOLOhKUOdPKMOnJxdtdqWGfOotuOB2R70','TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'],//'TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'
        success:res=>{
          console.log(res)
        },
        complete:()=>{
          wx.reLaunch({
            url: "/pages/Mall/Mall"
          });
        }
      })
    }
    if(index==2){
      wx.reLaunch({
        url: "/pages/cart/index/index"
      });
    }
    if(index==3){
      wx.reLaunch({
        url: "/pages/my/my-index/my-index"
      });
    }
  },
  gochain(){
    wx.reLaunch({
      url: '/pages/Chain/Chain'
    })
  },
  shopDetails(){
    //获取数据
    Controllers.shopDetails({shopId:wx.getStorageSync('shopId')}).then(res => {
      res.data.label=res.data.label.split('/')
      this.setData({
        shopDetailsdata:res.data
      })
      if(this.data.shopDetailsdata.typeName.indexOf('美业')!=-1){
        app.globalData.globalStype='pinStyle'
      }else{
        app.globalData.globalStype='yellowStyle'
      }
      this.setData({globalStype:app.globalData.globalStype})
      this.setData({
        tabbar: this.data.globalStype=='pinStyle'?app.globalData.tabbar:app.globalData.tabbar2,
      })
      this.shopRotationImg()
      this.shopGoodsPage()
    })
  },
  myInformation(){
    //获取数据
    Controllers.myInformation({shopId:this.data.shopId}).then(res => {
      this.setData({
        myInformationdata:res.data.accountRelation
      })
      app.globalData.myshopinfo=res.data.accountRelation
    })
  },
  getfixheight(){
    let query = wx.createSelectorQuery()
    var that=this;
    
      query.select('#store').boundingClientRect((rect) => {
        if(rect){
          let top = rect.top;
          that.setData({
            navheight:top
          })
        }
      }).exec()
 
    let systemInfo = wx.getSystemInfoSync()
    console.log(systemInfo.statusBarHeight)
    let barheight= (750 / systemInfo.windowWidth)*systemInfo.statusBarHeight;
    let pxToRpxScale = 750 / systemInfo.windowWidth;
    let navigationHeight = 44 * pxToRpxScale;
    this.setData({
      headerheight:navigationHeight,
      barheight
    })
  },
  shopRotationImg(){
    //获取数据
    Controllers.shopRotationImg({shopId:this.data.shopId,type:6}).then(res => {
      this.setData({
        shopRotationImgdata:res.data
      })
      this.getfixheight()
    }).catch(res=>{
      this.getfixheight()
    })

  },
  calling(e){
    var phone=e.currentTarget.dataset.phone
    app.calling(phone)
  },
  location(e){
    var lng=e.currentTarget.dataset.lng;
    var lat=e.currentTarget.dataset.lat;
    var name=e.currentTarget.dataset.name;
    var adress=e.currentTarget.dataset.adress;
    wx.openLocation({
    //当前经纬度
      latitude: Number(lat),
      longitude: Number(lng),
      //缩放级别默认28
      scale: 28,
      //位置名
      name: name,
      //详细地址
      address: adress,
      //成功打印信息
      success: function(res) {
          console.log(res)
      },
      //失败打印信息
      fail: function(err) {
          console.log(err)
      },
      //完成打印信息
      complete: function(info) {
          console.log(info)
      },
    })
  },
  shopGoodsPage(){
    Controllers.shopGoodsPage({
      pageSize:this.data.pageSize,
      pageNumber:this.data.pageNumber,
      type:this.data.type,
      shopId:this.data.shopId
    }).then(res => {
      if(res.data.list.length>0){
        for(let item of res.data.list){
          if(item.label){
            item.label=item.label.split('/')
          }
        }
        this.setData({
          shopGoodsPagedata:this.data.shopGoodsPagedata.concat(res.data.list),

        })
        
      }
      if(res.data.lastPage){
          this.setData({
            isEnd:true
          })
        }
      this.setData({isloading:false})
    }).catch(res=>{
      this.setData({isloading:false})
    })
  },
  changegoods(e){
    var type=e.currentTarget.dataset.type
    this.setData({
      shopGoodsPagedata:[],
      pageNumber:1,
      pageSize:5,
      isEnd:false,
      isloading:true,
      type
    })
    this.shopGoodsPage()
  },
  onReachBottom() {
    if (this.data.isEnd) {
      return;
    } else {
      this.setData({
          pageNumber: this.data.pageNumber + 1,
      });
      this.shopGoodsPage()
    }
  },
  onPageScroll: function (e) { // 获取滚动条当前位置
    if(this.data.navheight>0){

        if (this.data.navheight < e.scrollTop + 58) {
          this.setData({
            isFixedTop: true,
          })
          console.log(this.data.navheight - e.scrollTop - 58)
        } else {
          this.setData({
            isFixedTop: false
          })
        }

    }
  },
  tolists(){
    wx.navigateTo({
        url: "/pages/Index/Lists?name="+this.data.shopDetailsdata.shopName
    });
  },
  gointroduce(){
    wx.navigateTo({
      url: "/pages/Index/Introduce"
    });
  },
  onShareAppMessage: function (e) {
      let obj = {
        title: '',
        path:'/pages/Index/Index?shopId=' + this.data.shopId,
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
  },
  onShareTimeline: function (e) {
    let obj = {
      title: '',
      path:'/pages/Index/Index?shopId=' + this.data.shopId,
      success: function (res) {
        
      },
      fail(err) {
        
      }
    }
    return obj
  }
})